package com.xneure.database.dao;

import com.xneure.database.metaobject.FieldMetaObject;
import com.xneure.database.sql.ValueConvert;
import com.xneure.utils.ObjectUtil;

public class TableValueCompare{
	private DataBaseTable entity;
	private ValueConvert valConvert;
	private FieldMetaObject fieldMeta;

	public TableValueCompare(ValueConvert vc,Object entity){
		this.valConvert = vc;
		if(entity instanceof DataBaseTable){
			this.entity = (DataBaseTable)entity;
		}
	}

	public void setFieldMeta(FieldMetaObject fieldMeta){
		this.fieldMeta = fieldMeta;
	}

	public boolean compare(Object val){
		if(ObjectUtil.containNull(entity,entity.source)){
			return false;
		}
		Object tmp = entity.source.get(fieldMeta.getName());
		if(ObjectUtil.isNull(tmp)){
			return tmp == val;
		}else if(ObjectUtil.isNull(val)){
			return val == tmp;
		}else{
			return val.equals(valConvert.sqlValue(tmp));
		}
	}

	public Object getSourceValue(){
		if(ObjectUtil.containNull(entity,entity.source)){
			throw new NullPointerException();
		}
		return valConvert.sqlValue(fieldMeta,entity.source.get(fieldMeta.getName()));
	}
}
